/***************************************************************************
 *   Copyright (C) 2007 by Vishwajeet   *
 *   vishwajeet@qcomputingproject.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 ***************************************************************************/
#include "ImageLoder.h"

#include <QStringList>
//#include "math.h"

QImage scale(const QString &imageFileName)
{
  QImage image(imageFileName);
  return image;//image.scaled(QSize(imageSize, imageSize), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
}

ImageLoder::ImageLoder(QObject *parent)
    : QObject(parent)
{

  imageScaling = new QFutureWatcher<QImage>(this);
  connect(imageScaling, SIGNAL(resultReadyAt(int)), SLOT(doneLoading(int)));
  connect(imageScaling, SIGNAL(finished()), SLOT(finished()));
}


ImageLoder::~ImageLoder()
{
}

void ImageLoder::loadImages(const QStringList &list)
{
  // Cancel and wait if we are already loading images.
  if (imageScaling->isRunning())
    {
      imageScaling->cancel();
      imageScaling->waitForFinished();
    }

  if (list.count() == 0)
    {
      return;
    }

    // Use mapped to run the thread safe scale function on the files.
    imageScaling->setFuture(QtConcurrent::mapped(list, scale));

}

void ImageLoder::doneLoading(int num)
{
   imageStorage[num] = imageScaling->resultAt(num);

}

void ImageLoder::finished()
{

}


